<template>
  <view class="content">
    <!-- 顶部搜索框 -->
    <Navbar />
    <scroll-view class="main" scroll-y @scrolltolower="loadMore">
      <!-- 广告轮播图 -->
      <Carousel :banners="banners" height="280rpx" />
      <!-- 类目 -->
      <CateScroll :list="category" />
      <!--  时尚女装 -->
      <view class="panel recommend">
        <view class="item" v-for="item in hots" :key="item.id">
          <view class="title">
            {{ item.title }}<text>{{ item.alt }}</text>
          </view>
          <navigator
            hover-class="none"
            :url="`/pages/recommend/index?type=${item.type}`"
            class="cards"
          >
            <image
              mode="aspectFit"
              v-for="img in item.pictures"
              :key="img"
              :src="img"
            ></image>
          </navigator>
        </view>
      </view>
      <!-- 新鲜好物 -->
     
      <!-- 热门品牌 -->
     
      <!-- 猜你喜欢 -->
   
    </scroll-view>
  </view>
</template>

<script>
import {
  getBannerAPI,
  getCategoryAPI,
  getHotsAPI,
 
} from "@/api/home";
export default {
  data() {
    return {
      banners: [],
      // 4. 声明变量保存数据
      category: [],
      hots: [],
     
    
        // 翻页数据准备
        page: 1,
      totalPages: 0,
      isLoading: false,
    };
  },
  onLoad() {
    this.loadBanners();
    // 3. 加载后，触发请求函数
    this.loadCategory();
    // 取热门推荐
    this.getHots();
  
  },
  methods: {
    async loadBanners() {
      const { result } = await getBannerAPI();
      console.log(result);
      this.banners = result;
    },
    // 2. 封装请求函数，调用API
    async loadCategory() {
      const { result } = await getCategoryAPI();
      console.log(result);
      this.category = result;
    },
    async getHots() {
      const { result } = await getHotsAPI();
      this.hots = result;
      console.log(this.hots);
    },
  
    loadMore(){
       // 如果当前加载状态是 true 不能再次加载
       if(this.isLoading)return
         // 如果之前加载的页码已经等于总页数, 也不要继续加载了
      if (this.page >= this.totalPages) return;
      console.log('到底了');
      this.page++;
      this.getLikes();
    }
  },
};
</script>

<style lang="scss">
.content {
  height: 100vh;
  display: flex;
  flex-direction: column;
}
.main {
  flex: 1;
  overflow: hidden;
  background-color: #f7f7f8;
}
/* 公共面板 */
.panel {
  margin: 20rpx 20rpx 0;
  border-radius: 10rpx;
  background-color: #fff;
  .title {
    display: flex;
    align-items: center;
    padding: 24rpx 24rpx 0;
    font-size: 32rpx;
    color: #262626;
    position: relative;
    text {
      font-size: 24rpx;
      color: #7f7f7f;
      margin-left: 18rpx;
    }
  }
  .more {
    position: absolute;
    right: 24rpx;
    font-size: 26rpx;
    color: #7f7f7f;
  }
  .cards {
    display: flex;
  }
  .name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
/* 推荐专区 */
.recommend {
  display: flex;
  flex-wrap: wrap;
  .item {
    display: flex;
    flex-direction: column;
    width: 50%;
    height: 254rpx;
    border-right: 1rpx solid #eee;
    border-top: 1rpx solid #eee;
    &:nth-child(2n) {
      border-right: 0 none;
    }
    &:nth-child(-n + 2) {
      border-top: 0 none;
    }
  }
  .cards {
    flex: 1;
    padding: 15rpx 20rpx;
    justify-content: space-between;
    image {
      width: 48%;
    }
  }
}

/* 新鲜好物 */
.fresh {
  min-height: 320rpx;
  .price {
    line-height: 1;
    text-align: left;
    font-size: 26rpx;
    color: #cf4444;
  }
  .small {
    font-size: 80%;
  }
}
.fresh .cards navigator,
.brands .cards navigator {
  width: 25%;
  padding: 15rpx 20rpx 20rpx;
  line-height: 1;
  text-align: center;
}
.fresh image,
.brands image {
  width: 126rpx;
  height: 126rpx;
}
.fresh .name,
.brands .name {
  margin: 20rpx 0 16rpx;
  font-size: 24rpx;
  color: #262626;
}
.brands {
  .price {
    font-size: 24rpx;
    color: #999;
  }
}
</style>
